package com.pingpang.websocketchat;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import io.netty.channel.Channel;
 
public class ChannelManager {

	/*
	 * 用户  
	 */
	private static Map<ChartUser, Channel> userChannel = new ConcurrentHashMap <ChartUser, Channel>();

	/*
	 * 群聊群组
	 */
	private static Map<String, Set<ChartUser>> groupMap = new ConcurrentHashMap <String, Set<ChartUser>>();

	
	/**
	 * 通过用户代码获取用户信息
	 * @param userCode
	 * @return
	 */
	public static ChartUser getChartUser(String userCode) {
		if(!userChannel.isEmpty()) {
			for(ChartUser cu : userChannel.keySet()) {
				if(cu.getUserCode().equals(userCode)) {
					return cu;
				}
			}
		}
		return null;
	}
	
	
	/*
	 * 添加用户
	 */
	public static void addChannel(ChartUser chartUser, Channel ch) {
		System.out.println("添加用户:" + chartUser.getUserCode()+chartUser.getUserName()+ "IP:" + ch.remoteAddress());
		userChannel.put(chartUser, ch);
	}

	/*
	 * 获取用户
	 */
	public static Channel getChannel(String userCode) {
		return ChannelManager.userChannel.get(ChannelManager.getChartUser(userCode));
	}

	/*
	 * 移除用户
	 */
	public static void removeChannelByCode(String userCode) {
		System.out.println("移除用户:" + userCode);
		userChannel.remove(getChartUser(userCode));
		removeGroup(userCode);
	}

	/*
	 * 移除用户
	 */
	public static void removeChannelByChannel(Channel channel) {
		for (ChartUser key : userChannel.keySet()) {
			if (userChannel.get(key).equals(channel)) {
				System.out.println("移除用户:" + key + "IP:" + channel.remoteAddress());
				removeChannelByCode(key.getUserCode());
				removeGroup(key.getUserCode());
				return;
			}
		}
	}

	//获取所有用户信息
	public static Set<ChartUser> getAllUser(){
		return userChannel.keySet();
	}
	
	/*
	 * 添加群组
	 */
	public static void addGroup(String groupID, ChartUser cu) {
         if(null!=groupMap.get(groupID)) {
        	 groupMap.get(groupID).add(cu);
         }else {
        	 HashSet<ChartUser>set=new HashSet<ChartUser>();
        	 set.add(cu);
        	 groupMap.put(groupID, set);
         }
	}

	/**
	 * 移除群组
	 * @param groupID
	 * @param userCode
	 */
	public static void removeGroup(String groupID,String userCode) {
		if(null!=groupMap.get(groupID)) {
			ChartUser cu=new ChartUser();
			cu.setUserCode(userCode);
			groupMap.get(groupID).remove(cu);
		}
	}
	
	/**
	 * 移除群组
	 * @param userCode
	 */
	public static void removeGroup(String userCode) {
		for(String str:groupMap.keySet()) {
			ChartUser cu=new ChartUser();
			cu.setUserCode(userCode);
			groupMap.get(str).remove(userCode);
		}
	}
	
	/**
	 * 查找是否存在
	 * @param groupID
	 * @param userCode
	 * @return
	 */
	public static boolean isExit(String groupID,String userCode) {
		if(null!=groupMap.get(groupID)) {
			return groupMap.get(groupID).contains(userCode);
		}else {
			return false;
		}
	}
	
	/**
	 * 获取群组用户
	 * @param groupID
	 * @return
	 */
	public static Set<ChartUser> getGroupUser(String groupID){
		return groupMap.get(groupID);
	}
}
